/**
 *    @file       sockettask.h
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/13/2014 09:49:52 AM
 *
 *    @author     Theophilus (), wangtf418@gmail.com 
 */
#ifndef _SF_SOCKETTASK_H_
#define _SF_SOCKETTASK_H_
#include <map>
#include <list>
#include "sockbase.h"
#include "atomic_int.h"
#include "message.h"
#include "epoll.h"

#define SOCKTASK_TIMEOUT 60

class sockettask;
class netprocessthread;

typedef std::map<bf_uint64_t, sockettask*>::iterator sockettask_pos_t;

class sockettask
{
public:
	sockettask(csocket *sock, netprocessthread *netthread, const comargs *args = NULL);
	virtual ~sockettask();

public:
	virtual bf_int_t init(bf_time_t timeout = SOCKTASK_TIMEOUT, bf_int_t sock_buflen = 102400);
	virtual bf_int_t check_timeout();
	virtual bf_int_t destroy();
	
public:
	virtual bf_int_t proc_msg(message *msg) = 0;
	virtual bf_int_t put_send_packet(bf_char_t *buf, bf_size_t buflen);
	virtual bf_int_t process_io_event(bf_int_t events) = 0;

public:
	inline csocket * get_socket() {return m_socket;}
	inline bf_uint64_t get_taskid() {return m_taskid;}
	inline void set_pos(sockettask_pos_t & pos) {m_pos = pos;}
	inline sockettask_pos_t & get_pos() {return m_pos;}

protected:
	virtual bf_int_t recv_process() = 0;
	virtual bf_int_t send_process() = 0;
	virtual bf_int_t process_recv_packet(bf_char_t *buf, bf_size_t buflen) = 0;

protected:
	bf_int_t add_to_epoll(bf_int_t events = BF_EPOLL_IN);
	bf_int_t del_from_epoll();
	bf_int_t modify_epoll(bf_int_t events);
	
protected:
	csocket *m_socket;
	netprocessthread *m_netthread;
	bf_uint64_t m_taskid;
	sockettask_pos_t m_pos;
	bf_time_t m_last_activetime;
	bf_time_t m_timeout;
	std::list<send_message*> m_send_list;
	send_message *m_cur_sendmsg;
	comargs *m_args;
protected:
	static atomic_uint64 m_cur_taskid;
};


#endif

